package com.hdaccp.foodsales.service.impl;

import com.hdaccp.foodsales.dao.GonghuoDao;
import com.hdaccp.foodsales.entity.Gonghuo;
import com.hdaccp.foodsales.service.GonghuoService;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.util.CellRangeAddress;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class GonghuoServiceImpl implements GonghuoService {

    @Resource
    GonghuoDao gonghuoDao;

    @Override
    public Map<String, Object> getUserList(int pageNumber, int pageSize) {
        Map<String,Object>map=new HashMap<String, Object>();
        int count=gonghuoDao.getUserCount();
        List<Gonghuo> ulist=gonghuoDao.getUserList((pageNumber-1)*pageSize, pageNumber*pageSize);
        map.put("total", count);
        map.put("rows", ulist);
        return map;
    }



    public boolean validUserAccount(String account, int uid) {
        boolean flag=true;
        List<Gonghuo>ulist=gonghuoDao.getUserByAccount(account);
        if(uid!=0){//修改
            if(ulist.size()==1&&ulist.get(0).getGhid()==uid){
                flag=true;
            }else if(ulist.size()==0){
                flag=true;
            }else{
                flag=false;
            }
        }else{//添加
            if(ulist.size()>0){
                flag=false;
            }
        }
        return flag;
    }

    @Override
    public int saveUser(Gonghuo gonghuo, HttpServletRequest request) {
        String name=(String)request.getSession().getAttribute("NAME");
        int j=0;
        int i=gonghuoDao.saveUser(gonghuo);
        if(i>0){
            List<Gonghuo>list=gonghuoDao.getUserByAccount(gonghuo.getGhuo());

            j=gonghuoDao.insertUserOpt(list.get(0).getGhid(), "添加", name);
            if(j==1){
                j=1;
            }else{
                j=0;
            }
        }
        return j;
    }

    @Override
    public int delUser(int uid) {
        int j=0;
        int i=gonghuoDao.delUserOpt(uid);
        if(i>0){
            j=gonghuoDao.delUser(uid);
        }
        return j;
    }

    @Override
    public int delUsers(int[] ids) {
        int sum=0;
        int y=0;
        for(int i=0;i<ids.length;i++){
            int j=gonghuoDao.delUserOpt(ids[i]);
            if(j>0){
                int x=gonghuoDao.delUser(ids[i]);
                sum+=x;
            }
        }
        if(sum==ids.length){
            y=1;
        }
        return y;
    }

    @Override
    public Gonghuo queryUserById(int id) {
        Gonghuo gonghuo=gonghuoDao.queryUserById(id);
        return gonghuo;
    }

    @Override
    public void ExportUserList(HttpServletResponse response) {
        //HSSFWorkbook对象(excel的文档对象)
        HSSFWorkbook workBook=new HSSFWorkbook();

        //sheet对象（excel的表单）
        HSSFSheet sheet=workBook.createSheet("用户表信息");

        //行数，参数为行索引(excel的行)
        HSSFRow rowHead=sheet.createRow(0);//第一行  可以是0～65535之间的任何一个

        //创建excel的单元格，参数为列索引，可以是0～255之间的任何一个
        HSSFCell cellOne=rowHead.createCell(0);
        cellOne.setCellValue("用户信息");//表头

        //合并单元格CellRangeAddress构造参数依次表示起始行，截至行，起始列， 截至列
        sheet.addMergedRegion(new CellRangeAddress(0,0,0,9));

        HSSFRow rows=sheet.createRow(1);//第二行
        //创建单元格并设置单元格内容
        rows.createCell(0).setCellValue("供货商编号");
        rows.createCell(1).setCellValue("供货商名称");
        rows.createCell(2).setCellValue("联系人");
        rows.createCell(3).setCellValue("地址");
        rows.createCell(4).setCellValue("备注");
        rows.createCell(5).setCellValue("电话");


        List<Gonghuo>userList=gonghuoDao.queryUserList();
        for(int i=0;i<userList.size();i++){
            HSSFRow row=sheet.createRow(i+2);//从第三行开始
            //创建单元格并设置单元格内容
            row.createCell(0).setCellValue(userList.get(i).getGhid());
            row.createCell(1).setCellValue(userList.get(i).getGhuo());
            row.createCell(2).setCellValue(userList.get(i).getGhxm());
            row.createCell(3).setCellValue(userList.get(i).getGhdizhi());
            row.createCell(4).setCellValue(userList.get(i).getGhbeizhu());
            row.createCell(5).setCellValue(userList.get(i).getGhphone());

        }
        //输出Excel文件
        try {
            OutputStream output=response.getOutputStream();
            response.reset();
            response.setHeader("Content-disposition", "attachment; filename=userinfo.xls");
            response.setContentType("application/msexcel");
            workBook.write(output);
            output.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    @Override
    public Map<String, Object> getUserListByCon(int spage, int epage, int ghid, String ghxm, int deptId) {
        List<Gonghuo> userList=gonghuoDao.getUserListByCon(spage, epage,ghid, ghxm, deptId);
        int count=gonghuoDao.getContByCon(ghid, ghxm, deptId);
        Map<String,Object> map=new HashMap<String, Object>();
        map.put("rows", userList);
        map.put("total", count);
        return map;
    }
}
